package org.wu.framework.lazy.orm.database.sql.expand.database.persistence.factory;

import org.wu.framework.lazy.orm.core.config.LazyOperationAttribute;
import org.wu.framework.lazy.orm.core.config.enums.LazyDataSourceType;
import org.wu.framework.lazy.orm.core.config.prop.DefaultLazyDataSourceAttribute;
import org.wu.framework.lazy.orm.core.config.prop.LazyDataSourceAttribute;
import org.wu.framework.lazy.orm.core.source.adapter.SourceFactory;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import org.wu.framework.lazy.orm.database.sql.expand.database.persistence.LazySqlOperation;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * describe : LazyLambdaStream 工厂
 *
 * @author : Jia wei Wu
 * @version 1.0
 * @date : 2022/6/25 15:31
 */
public final class LazyLambdaStreamFactory {


    /**
     * 创建 LazyLambdaStream 对象
     *
     * @param lazySqlOperation 懒人数据库持久层操作合集
     * @return LazyLambdaStream
     */
    public static LazyLambdaStream createLazyLambdaStream(LazySqlOperation lazySqlOperation) {
        return new LazyLambdaStream(lazySqlOperation);
    }

    /**
     * 创建 LazyLambdaStream 对象
     *
     * @param alias  数据源别名
     * @param dataSource 数据源
     * @param lazyOperationAttribute 配置
     * @return LazyLambdaStream
     */
    public static LazyLambdaStream createLazyLambdaStream(String alias,DataSource dataSource,LazyOperationAttribute lazyOperationAttribute) {
        Map<String,DataSource> dataSourceMap = new HashMap<>();
        dataSourceMap.put(alias, dataSource);
        LazySqlOperation lazySqlOperation = LazyOperationProxyFactory.createLazyOperation(dataSourceMap,lazyOperationAttribute);
        return createLazyLambdaStream(lazySqlOperation);
    }

    /**
     * 创建 LazyLambdaStream 对象
     *
     * @param alias  数据源别名
     * @param dataSource 数据源
     * @return LazyLambdaStream
     */
    public static LazyLambdaStream createLazyLambdaStream(String alias,DataSource dataSource) {
        return createLazyLambdaStream(alias,dataSource,null);
    }

    /**
     * 创建 LazyLambdaStream 对象
     *
     * @param lazyDataSourceAttribute 配置信息
     * @return LazyLambdaStream
     */
    public static LazyLambdaStream createLazyLambdaStream(LazyDataSourceAttribute lazyDataSourceAttribute, LazyOperationAttribute lazyOperationAttribute) {
        LazySqlOperation lazySqlOperation = LazyOperationProxyFactory.createLazyOperation(lazyDataSourceAttribute, lazyOperationAttribute);
        return createLazyLambdaStream(lazySqlOperation);
    }

    /**
     * 创建 LazyLambdaStream 对象
     *
     * @param lazyDataSourceAttribute 配置信息
     * @return LazyLambdaStream
     */
    public static LazyLambdaStream createLazyLambdaStream(LazyDataSourceAttribute lazyDataSourceAttribute) {
        return createLazyLambdaStream(lazyDataSourceAttribute, null);
    }

    /**
     * 创建 LazyLambdaStream 对象
     *
     * @param url      数据库地址
     * @param username 用户名称
     * @param password 用户密码
     * @return LazyLambdaStream
     */
    public static LazyLambdaStream createLazyLambdaStream(String url, String username, String password) {
        return createLazyLambdaStream(url, username, password,"master");
    }
    /**
     * 创建 LazyLambdaStream 对象
     *
     * @param url      数据库地址
     * @param username 用户名称
     * @param password 用户密码
     * @return LazyLambdaStream
     */
    public static LazyLambdaStream createLazyLambdaStream(String url, String username, String password,String alias) {
        LazyDataSourceAttribute lazyDataSourceAttribute = new DefaultLazyDataSourceAttribute();
        lazyDataSourceAttribute.setUrl(url);
        lazyDataSourceAttribute.setUsername(username);
        lazyDataSourceAttribute.setPassword(password);
        lazyDataSourceAttribute.setAlias(alias);
        return createLazyLambdaStream(lazyDataSourceAttribute);
    }

    /**
     * 创建 LazyLambdaStream 对象
     *
     * @param url                数据库地址
     * @param username           用户名称
     * @param password           用户密码
     * @param type               数据源类型
     * @param alias              数据源别名
     * @param lazyDataSourceType 数据源类型
     * @return LazyLambdaStream
     */
    public static LazyLambdaStream createLazyLambdaStream(String url,
                                                          String username,
                                                          String password,
                                                          Class<? extends DataSource> type,
                                                          String alias,
                                                          LazyDataSourceType lazyDataSourceType

    ) {
        LazyDataSourceAttribute lazyDataSourceAttribute = new DefaultLazyDataSourceAttribute();
        lazyDataSourceAttribute.setUrl(url);
        lazyDataSourceAttribute.setUsername(username);
        lazyDataSourceAttribute.setPassword(password);
        lazyDataSourceAttribute.setType(type);
        lazyDataSourceAttribute.setAlias(alias);
        lazyDataSourceAttribute.setLazyDataSourceType(lazyDataSourceType);
        return createLazyLambdaStream(lazyDataSourceAttribute);
    }

    /**
     * 创建 LazyLambdaStream 对象
     *
     * @param url      数据库地址
     * @param username 用户名称
     * @param password 用户密码
     * @param type     数据源类型
     * @return LazyLambdaStream
     */
    public static LazyLambdaStream createLazyLambdaStream(String url, String username, String password, Class<? extends DataSource> type, LazyDataSourceType lazyDataSourceType) {
        return createLazyLambdaStream(url, username, password, type, "master", lazyDataSourceType);
    }

    /**
     * 创建 LazyLambdaStream 对象
     *
     * @param url      数据库地址
     * @param username 用户名称
     * @param password 用户密码
     * @param type     数据源类型
     * @return LazyLambdaStream
     */
    public static LazyLambdaStream createLazyLambdaStream(String url, String username, String password, Class<? extends DataSource> type) {
        return createLazyLambdaStream(url, username, password, type, LazyDataSourceType.MySQL);
    }

    /**
     * 创建 LazyLambdaStream 对象
     *
     * @param host     数据库IP
     * @param port     数据库端口
     * @param username 用户名称
     * @param password 用户密码
     * @return LazyLambdaStream
     */
    public static LazyLambdaStream createLazyLambdaStream(String host, int port, String schema, String username, String password) {
        return createLazyLambdaStream(host, port, schema, username, password, null);
    }

    /**
     * 创建 LazyLambdaStream 对象
     *
     * @param host     数据库IP
     * @param port     数据库端口
     * @param username 用户名称
     * @param password 用户密码
     * @param type     数据源类型
     * @return LazyLambdaStream
     */
    public static LazyLambdaStream createLazyLambdaStream(String host, int port, String schema, String username, String password, Class<? extends DataSource> type) {
        return createLazyLambdaStream(host, port, schema, username, password, type, LazyDataSourceType.MySQL);
    }

    /**
     * 创建 LazyLambdaStream 对象
     *
     * @param host     数据库IP
     * @param port     数据库端口
     * @param username 用户名称
     * @param password 用户密码
     * @param type     数据源类型
     * @return LazyLambdaStream
     */
    public static LazyLambdaStream createLazyLambdaStream(String host,
                                                          int port,
                                                          String schema,
                                                          String username,
                                                          String password,
                                                          Class<? extends DataSource> type,
                                                          LazyDataSourceType lazyDataSourceType
    ) {
        String url = SourceFactory.getUrl(lazyDataSourceType, host, port, schema);
        return createLazyLambdaStream(url, username, password, type,lazyDataSourceType);
    }
}
